function pajax(options) {

    return new Promise(function (resolve, reject) {

        options.success = function (response) {
            resolve(response)
        }
        ajax(options)
    });
}

function ajax(options) {

    let config = {
        url: '',
        type: 'get',
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded',
        data: '',
        success: function () {

        }
    }

    for (let key in config) {
        if (options[key]) {

            config[key] = options[key];
        }
    }



    if (!config.url) {
        throw new Error('URL地址必须传递');
    }

    config.type = config.type.toLowerCase();
    let allowType = ['get', 'post', 'put', 'delete'];

    if (!allowType.includes(config.type)) {

        throw new Error('请求方式只能是' + allowType)
    }

    if (Object.prototype.toString.call(config.success) != '[object Function]') {
        throw new Error('成功的回调必须是一个函数');
    }


    let dataType = Object.prototype.toString.call(config.data);
    if (dataType != '[object String]' && dataType != '[object Object]') {
        throw new Error('参数数据类型错误')
    }

    let xhr = new XMLHttpRequest;

    if (config.type == 'get' || config.type == 'delete') {
        let data = config.data;
        if (dataType == '[object Object]') {
            let str = '';

            for (let k in data) {
                str += `${k}=${data[k]}&`;
            }

            data = str;
        }
        xhr.open(config.type, config.url + '?' + data);
    } else {
        xhr.open(config.type, config.url);
    }


    xhr.setRequestHeader('Content-Type', config.contentType);

    if (config.type == 'post' || config.type == 'put') {
        let data = config.data;
        if (dataType == '[object Object]' && config.contentType == 'application/x-www-form-urlencoded') {
            let str = '';

            for (let k in data) {
                str += `${k}=${data[k]}&`;
            }

            data = str.slice(0, -1);
        } else if (dataType == '[object Object]' && config.contentType == 'application/json') {
            data = JSON.stringify(config.data);
        }

        xhr.send(data);
    } else {

        xhr.send();
    }

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {

            let response = (config.dataType == 'json') ? JSON.parse(xhr.responseText) : xhr.responseText;
            config.success(response)
        }
    }





}

/**
 * 设置cookie
 * @param {String} key  cookie的名称 
 * @param {String} value  cookie对应的值 
 * @param {Number} expires  表示cookie的失效时间 需要是数字 表示多少秒之后过期，默认值为0 表示关闭浏览器失效 
 */
function setCookie(key, value, expires = 0) {
    if (expires == 0) {
        document.cookie = `${key}=${value};path=/`;
    } else {
        let d = new Date;
        d.setTime(d.getTime() + expires * 1000 - 8 * 3600 * 1000);
        document.cookie = `${key}=${value};path=/;expires=` + d;
    }
}

/**
 * 获取cookie
 * @param {String} key  cookie的名称
 */
function getCookie(key) {
    let data = '';
    document.cookie.split('; ').forEach(item => {
        let keyValue = item.split('=')
        if (keyValue[0] == key) {
            data = keyValue[1]
        }
    })
    return data;
}

/**
 * 删除cookie
 * @param {String} key cookie的名称 
 */
function removeCookie(key) {
    setCookie(key, '', -1);
}

/**
 * 获取到URL地址上指定参数的值
 */
function getUrlParam(name) {
    let value = '';
    location.search.substr(1).split('&').forEach(item => {
        let keyValue = item.split('=');
        if (keyValue[0] == name) {
            value = keyValue[1]
        }
    })
    return value;
}